package github.kawaiior.yuanchuxiuzhen.common.container;

import github.kawaiior.yuanchuxiuzhen.common.item.misc.ManaStoneItem;
import github.kawaiior.yuanchuxiuzhen.common.tileentity.GongFaCreateTileEntity;
import github.kawaiior.yuanchuxiuzhen.common.tileentity.ManaStoneConverterTileEntity;
import github.kawaiior.yuanchuxiuzhen.core.init.BlockInit;
import github.kawaiior.yuanchuxiuzhen.core.init.ContainerInit;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.ClickType;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.ContainerType;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IWorldPosCallable;
import net.minecraft.util.math.BlockPos;

import java.util.Objects;

public class ManaStoneConverterContainer extends Container {

    public final ManaStoneConverterTileEntity tileEntity;

    private final IWorldPosCallable ableToInteract;

    public ManaStoneConverterContainer(final int id, final PlayerInventory playerInventory, final ManaStoneConverterTileEntity entity){
        super(ContainerInit.MANA_STONE_CONVERTER_CONTAINER.get(), id);
        this.tileEntity = entity;
        this.ableToInteract = IWorldPosCallable.of(Objects.requireNonNull(tileEntity.getWorld()),tileEntity.getPos());

        this.addSlot(new Slot(entity.getItemInventory(), 0, 80, 12));
        this.addSlot(new Slot(entity.getManaInventory(), 0, 80, 42));

        // add player inventory
        for(int i =0;i<9;i++){
            this.addSlot(new Slot(playerInventory,i,i*18+8,152-10));
            this.addSlot(new Slot(playerInventory,i+9,i*18+8,94-10));
            this.addSlot(new Slot(playerInventory,i+18,i*18+8,112-10));
            this.addSlot(new Slot(playerInventory,i+27,i*18+8,130-10));
        }
    }

    public ManaStoneConverterContainer(final int id, final PlayerInventory playerInventory, final PacketBuffer data) {
        this(id, playerInventory, getTileEntity(playerInventory,data));
    }

    public static ManaStoneConverterTileEntity getTileEntity(PlayerInventory playerInventory, PacketBuffer data){
        Objects.requireNonNull(playerInventory,"玩家物品栏不能为空");
        Objects.requireNonNull(data,"数据包不能为空");

        BlockPos blockPos = data.readBlockPos();
        TileEntity tileEntity = playerInventory.player.world.getTileEntity(blockPos);

        if(tileEntity instanceof ManaStoneConverterTileEntity){
            return (ManaStoneConverterTileEntity) tileEntity;
        }
        throw new IllegalStateException("invalid tile entity");
    }


    @Override
    public boolean canInteractWith(PlayerEntity playerIn) {
        return isWithinUsableDistance(ableToInteract, playerIn, BlockInit.MANA_STONE_CONVERTER_BLOCK.get());
    }

    @Override
    public ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity player) {

        if (clickTypeIn == ClickType.PICKUP){
            return pickup(slotId, dragType, clickTypeIn, player);
        }else if (clickTypeIn == ClickType.QUICK_MOVE){
            return quickMove(slotId, player);
        }else if (clickTypeIn == ClickType.PICKUP_ALL){
            return ItemStack.EMPTY;
        }else if (clickTypeIn == ClickType.QUICK_CRAFT){
            return player.inventory.getItemStack();
        }

        return super.slotClick(slotId, dragType, clickTypeIn, player);
    }

    private ItemStack pickup(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity player){
        if (slotId > 1){
            return super.slotClick(slotId, dragType, clickTypeIn, player);
        }

        ItemStack playerInvStack = player.inventory.getItemStack();
        if (playerInvStack.isEmpty()){
            if (slotId==0){
                return super.slotClick(slotId, dragType, clickTypeIn, player);
            }

            ItemStack manaStack = this.inventorySlots.get(slotId).getStack();
            ItemStack stack = this.inventorySlots.get(0).getStack();

            if (stack.getCount() > 0){
                stack.shrink(1);
                player.inventory.setItemStack(manaStack.copy());
                return playerInvStack;
            }
        }else {
            if (slotId==1){
                ItemStack stack = this.inventorySlots.get(0).getStack();
                ItemStack manaStack = this.inventorySlots.get(slotId).getStack();
                if (manaStack.getItem()==playerInvStack.getItem()){
                    if (playerInvStack.getCount()<playerInvStack.getMaxStackSize()) {
                        stack.shrink(1);
                        playerInvStack.setCount(playerInvStack.getCount() + 1);
                    }
                }
                return playerInvStack;
            }
        }

        return super.slotClick(slotId, dragType, clickTypeIn, player);
    }

    private ItemStack quickMove(int slotId, PlayerEntity player){

        return ItemStack.EMPTY;
    }

}
